package javafx.FX3D_camera;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SubScene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

/**
 *
 * @author Administrator
 */
public class FX3D_camera extends Application {
    
    int up_rotate_count=0;
    int left_rotate_count=0;
    Rotate up_rotate;
    Rotate left_rotate;
    PerspectiveCamera camera=new PerspectiveCamera(true);
    @Override
    public void start(Stage primaryStage)throws Exception{
        Scene scene = new Scene(createContent());
        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent event) {
                switch(event.getCode()){
                    case W:
                        up_rotate_count+=1;
                        if(!camera.getTransforms().isEmpty()){
                            camera.getTransforms().remove(up_rotate);
                            camera.getTransforms().remove(left_rotate);
                        }
                        up_rotate=new Rotate(up_rotate_count*2, Rotate.X_AXIS);
                        left_rotate=new Rotate(left_rotate_count*2, Rotate.Y_AXIS);
                        
                        camera.getTransforms().addAll(up_rotate,left_rotate);
                        break;
                    case S:
                        up_rotate_count-=1;
                        if(!camera.getTransforms().isEmpty()){
                            camera.getTransforms().remove(up_rotate);
                            camera.getTransforms().remove(left_rotate);
                        }
                        up_rotate=new Rotate(up_rotate_count*2, Rotate.X_AXIS);
                        left_rotate=new Rotate(left_rotate_count*2, Rotate.Y_AXIS);
                        
                        camera.getTransforms().addAll(up_rotate,left_rotate);
                        break;
                    case A:
                        left_rotate_count-=1;
                        if(!camera.getTransforms().isEmpty()){
                            camera.getTransforms().remove(up_rotate);
                            camera.getTransforms().remove(left_rotate);
                        }
                        up_rotate=new Rotate(up_rotate_count*2, Rotate.X_AXIS);
                        left_rotate=new Rotate(left_rotate_count*2, Rotate.Y_AXIS);
                        
                        camera.getTransforms().addAll(up_rotate,left_rotate);
                        break;
                    case D:
                        left_rotate_count+=1;
                        if(!camera.getTransforms().isEmpty()){
                            camera.getTransforms().remove(up_rotate);
                            camera.getTransforms().remove(left_rotate);
                        }
                        up_rotate=new Rotate(up_rotate_count*2, Rotate.X_AXIS);
                        left_rotate=new Rotate(left_rotate_count*2, Rotate.Y_AXIS);
                        
                        camera.getTransforms().addAll(up_rotate,left_rotate);
                        break;
                }
            }
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public Parent createContent() throws Exception{
        Box skyBox_up=new Box(50, 50, 50);
        Box skyBox_down=new Box(50, 50, 50);
        Box skyBox_left=new Box(50, 50, 50);
        Box skyBox_right=new Box(50, 50, 50);
        Box skyBox_front=new Box(50, 50, 50);
        Box skyBox_back=new Box(50, 50, 50);
        
        PhongMaterial material_up=new PhongMaterial(Color.ALICEBLUE);
        material_up.setDiffuseMap(new Image("image/glacier_up_rotate.jpg"));
        PhongMaterial material_down=new PhongMaterial(Color.ALICEBLUE);
        material_down.setDiffuseMap(new Image("image/glacier_down_rotate.jpg"));
        PhongMaterial material_left=new PhongMaterial(Color.ALICEBLUE);
        material_left.setDiffuseMap(new Image("image/glacier_west.jpg"));
        PhongMaterial material_right=new PhongMaterial(Color.ALICEBLUE);
        material_right.setDiffuseMap(new Image("image/glacier_east.jpg"));
        PhongMaterial material_front=new PhongMaterial(Color.ALICEBLUE);
        material_front.setDiffuseMap(new Image("image/glacier_north.jpg"));
        PhongMaterial material_back=new PhongMaterial(Color.ALICEBLUE);
        material_back.setDiffuseMap(new Image("image/glacier_south.jpg"));
        
        //PhongMaterial material=new PhongMaterial(Color.ALICEBLUE);
        //material.setDiffuseMap(new Image("image/Glacier.JPG"));
        //material.setSpecularMap(new Image("image/2.jpg"));
        skyBox_up.setMaterial(material_up);
        skyBox_down.setMaterial(material_down);
        skyBox_left.setMaterial(material_left);
        skyBox_right.setMaterial(material_right);
        skyBox_front.setMaterial(material_front);
        skyBox_back.setMaterial(material_back);
        
        //huanzuobiao
        skyBox_up.getTransforms().add(new Translate(0, -50, 0));//这个却是向左？
        skyBox_down.getTransforms().add(new Translate(0, 50, 0));
        skyBox_left.getTransforms().add(new Translate(-50, 0, 0));//这个却是向？
        skyBox_right.getTransforms().add(new Translate(50, 0, 0));
        skyBox_front.getTransforms().add(new Translate(0, 0, 50));//这个却是向？
        skyBox_back.getTransforms().add(new Translate(0, 0, -50));
        
        
        Group root=new Group();
        root.getChildren().add(camera);
        root.getChildren().addAll(skyBox_up,skyBox_down,skyBox_left,skyBox_right,skyBox_front,skyBox_back);
        
        
        
        SubScene subScene=new SubScene(root, 768, 768);
        subScene.setFill(Color.CADETBLUE);

        subScene.setCamera(camera);
        Group group=new Group();
        group.getChildren().add(subScene);
        return group;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}
